<?php defined('SYSPATH') or die('No direct access allowed.');

class Controller_Admin_Auth extends Controller_Template
{
    public $template = "admin/login";
    
    public function action_login()
    {        
        if($_POST && Request::$initial->is_ajax())
        { 
            
            $this->auto_render = false;
            $response = array();
            
            if(Auth::instance()->login($_POST['login'], $_POST['pass'])){
                $response = array('status' => 'ok');
                $session = Session::instance();
                if($redirect = $session->get('redirect')) {
                    $session->delete('redirect');                
                    $response['url'] = $redirect;
                } else {
                    $response['url'] = 'admin/main';
                }
            } else {
                $response = array('status' => 'error',
                                  'error' => '<b>Ой, ошибочка вышла...</b><br/>Неверная пара логин/пароль');
                
               
            }
            
            
             
             
            
            $this->response->body(json_encode($response));
        }
                
    }
    
    public function action_logout()
    {
        Auth::instance()->logout(true);
        Request::$initial->redirect('admin/main');
    }
}